package com.spy.system.service.impl;

import java.util.List;



import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.util.StringUtil;
import com.spy.common.constant.HttpStatus;
import com.spy.common.core.page.TableDataInfo;
import com.spy.common.utils.DateUtils;
import com.spy.common.utils.StringUtils;
import com.spy.material.domain.Inventory;
import com.spy.material.mapper.InventoryMapper;
import com.spy.system.dto.MatCategoryPageReqDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spy.system.mapper.MatCategoryMapper;
import com.spy.system.domain.MatCategory;
import com.spy.system.service.IMatCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
/**
 * 物料类别Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-09
 */
@Service
public class MatCategoryServiceImpl extends ServiceImpl<MatCategoryMapper, MatCategory> implements IMatCategoryService
{
    @Autowired
    private MatCategoryMapper matCategoryMapper;
    @Autowired
    private InventoryMapper inventoryMapper;

    /**
     * 查询物料类别
     * 
     * @param id 物料类别主键
     * @return 物料类别
     */
    @Override
    public MatCategory selectMatCategoryById(Long id)
    {
                return getById(id);
    }

    /**
     * 查询物料类别列表
     * 
     * @param mcpdto 物料类别
     * @return 物料类别
     */
    @Override
    public TableDataInfo selectMatCategoryList(MatCategoryPageReqDto mcpdto)
    {
        Page page = new Page(mcpdto.getPageNum(), mcpdto.getPageSize());
        LambdaQueryWrapper<MatCategory> wrapper=new LambdaQueryWrapper<>();
        if (mcpdto.getMaterialCategory()!=null && mcpdto.getMaterialCategory()!=" ") {
            wrapper.like(MatCategory::getMaterialCategory,mcpdto.getMaterialCategory());
        }
        if (mcpdto.getStatus()!=null){
            wrapper.eq(MatCategory::getStatus,mcpdto.getStatus());
        }

        Page page1 = page(page, wrapper);

        TableDataInfo tableDataInfo=new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(page1.getRecords());
        tableDataInfo.setTotal(page1.getTotal());
        return  tableDataInfo;
    }

    /**
     * 新增物料类别
     * 
     * @param matCategory 物料类别
     * @return 结果
     */
    @Override
    public int insertMatCategory(MatCategory matCategory)
    {
        if (StringUtils.isEmpty(matCategory.getMaterialCategory())){
            throw new RuntimeException("物料类别不能为空");
        }
        //防止重复插入
        LambdaQueryWrapper<MatCategory> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(MatCategory::getMaterialCategory,matCategory.getMaterialCategory());
        long count = count(wrapper);
        if (count>0) {
            throw new RuntimeException("物料类别已存在：" + matCategory.getMaterialCategory());
        }
        //设置默认值
        if (matCategory.getStatus()==null && matCategory.getStatus()==0){
            matCategory.setStatus(1);
        }
         //matCategoryMapper.insertMatCategory(matCategory);
        int i =matCategoryMapper.insert(matCategory);
        return i;
    }

    /**
     * 修改物料类别
     * 
     * @param matCategory 物料类别
     * @return 结果
     */
    @Override
    public int updateMatCategory(MatCategory matCategory)
    {
        //根据库存表物料类别是否为空，来跟新物料类别表的状态
        if (matCategory.getMaterialCategory()==null || matCategory.getMaterialCategory()==" "){
            throw new RuntimeException("物料类别不能为空");
        }
        LambdaQueryWrapper<Inventory> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Inventory::getMaterialCategory,matCategory.getMaterialCategory());
        Long count = inventoryMapper.selectCount(wrapper);
        if (count>0){
            matCategory.setStatus(1);
        }else {
            matCategory.setStatus(0);
        }
        boolean updated = this.updateById(matCategory);
        return updated ? 1 : 0;
    }

    /**
     * 批量删除物料类别
     * 
     * @param ids 需要删除的物料类别主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteMatCategoryByIds(Long[] ids)
    {
        if (ids == null || ids.length == 0) {
        return 0;
    }
        // 检查每个要删除的类别是否正在被库存使用
        for (Long id : ids) {
            MatCategory category = this.getById(id);
            if (category == null) continue;

            Long count = inventoryMapper.selectCount(
                    new LambdaQueryWrapper<Inventory>()
                            .eq(Inventory::getMaterialCategory, category.getMaterialCategory())
            );
            if (count != null && count > 0) {
                throw new RuntimeException("物料类别【" + category.getMaterialCategory() + "】正在被库存使用，不能删除！");
            }
        }
        boolean success = this.removeByIds(Arrays.asList(ids));
        return success ? 1 : 0;
    }

    /**
     * 删除物料类别信息
     * 
     * @param id 物料类别主键
     * @return 结果
     */
    @Override
    public int deleteMatCategoryById(Long id)
    {
                return removeById(id) ? 1 : 0;
    }
}
